<?php

namespace app\common\helpers;

use think\Exception;

class PayHelper
{

    private static $instance = null;

    // 目标充值类
    private $target_class = null;

    // 目标渠道号
    private $target_channel = null;

    private function __construct(){}

    private function __clone(){}

    public static function getInstance($channel){
        if(self::$instance == null){

            try {
                $debug_stack = debug_backtrace();
                // 根据调用堆栈获取上个调用者类名称
                $path = array_shift($debug_stack)['file'];
                $file_arr = explode('/',str_replace('.php','',$path));
                if(empty($file_arr)){
                    throw new Exception('找不到类文件');
                }
            } catch (Exception $e) {
                mylog($e->getMessage());
                return false;
            }

            $class_name = end($file_arr);

            self::$instance = new self();
            self::$instance->target_class = $class_name;
            self::$instance->target_channel = $channel;
        }

        return self::$instance;
    }

    public function record($data = '')
    {
        $time = date('Y-m-d H:i:s', time());

        $dir = ROOT_PATH . 'runtime' . DS . 'paylog' . DS;

        if (!is_dir($dir)) {
            mkdir($dir);
        }

        $log_file = $dir . $this->target_channel.$this->target_class . '.txt';
        if (empty($data)) {
            $data = '';
        } else if (is_array($data) or is_object($data)) {
            $data = urldecode(http_build_query($data));
        }

        file_put_contents(strtolower($log_file), $time . ' ' . $data .  PHP_EOL, FILE_APPEND);
    }

    public function curl_post($url = null, $param = null,$type = 'form',$basic = null)
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        if($type == 'form'){
            $curlPost = http_build_query($param);
            $headers = [
                "Content-Type: application/x-www-form-urlencoded",
            ];
        }else{
            $curlPost = json_encode($param);
            $headers = [
                "Content-Type: application/json; charset=UTF-8",
            ];
        }

        if($basic != null){
            array_push($headers,"Authorization: Basic ".$basic);
            self::record($headers);
        }

        try {
            $curl = curl_init(); //初始化curl
            curl_setopt($curl, CURLOPT_URL, $url); //抓取指定网页
            // curl_setopt($curl, CURLOPT_HEADER, 0); //设置header
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
            curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
            curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); //提交的参数
            $data = curl_exec($curl); //运行curl
            curl_close($curl);

            if($data === false){
                throw new Exception('curl没有返回');
            }

            return $data;
        }catch (Exception $e){
            self::record('exception:'.$e->getFile().' '.$e->getLine().' '.$e->getMessage());
            return false;
        }
    }

}
